﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModSalesman
{
    public partial class SalesmanAdd : System.Web.UI.Page
    {
        SalesmanBiz salesmanBiz = new SalesmanBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        string saleManID = "";
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.QueryString["SaleManID"] != null)
            {
                saleManID = Request.QueryString["SaleManID"].ToString();
            }
           
             if (!IsPostBack)
            {               
                JSUtility.AddConfirm(btnDelete, "您真的要删除该员工的信息吗？");
                if (saleManID != "")
                {
                    BindDataByID(saleManID);
                }
                else
                { 
                txtJoinTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                defDicBiz.InitDropDownList("Admin_Salesman_Type", ddlTypeName, "长期");
                defDicBiz.InitDropDownList("Admin_Emp_Education", ddlEducationID, "高中");
              
             }}
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            string typeName= ddlTypeName.SelectedItem.Text.Trim();
            if (typeName == "请选择")
            {
                JSUtility.Alert(this, "请选择工作类别！");
                return;
            }
            string educationHigh = ddlEducationID.SelectedItem.Text.Trim();
            if (educationHigh == "请选择")
            {
                JSUtility.Alert(this, "请选择最高学历！");
                return;
            }
            string stateName = ddlStateName.SelectedItem.Text.Trim();
            string outTime = txtOutTime.Text.Trim();
            if (stateName == "在职")
            {
                //判断有没有重复
                if (salesmanBiz.IsExistEmpCode(empCode, "在职"))
                {
                    JSUtility.Alert(this, "该售货员还在职，必须先让使用此工号的人离职");
                    return;
                }
            }
            else
            {                
                    JSUtility.Alert(this, "选择了离职状态，不能新增！");
                    return;       
            }
           

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("EmpSex", ddlEmpSex.SelectedItem.Text.Trim());
            sdList.Add("Registry", txtRegistry.Text.Trim());
            sdList.Add("Nationality", txtNationality.Text.Trim());
            sdList.Add("ZipCode", txtZipCode.Text.Trim());
            sdList.Add("Addr", txtAddr.Text.Trim());
            sdList.Add("HomeTel", txtHomeTel.Text.Trim());
            sdList.Add("Mobile", txtMobile.Text.Trim());
            sdList.Add("EducationSchool", txtEducationSchool.Text.Trim());
            sdList.Add("EducationHigh", educationHigh);
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtBrand.Text.Trim());
            sdList.Add("TypeName", typeName);
            sdList.Add("StateName", stateName);           
            sdList.Add("JoinTime", txtJoinTime.Text.Trim());
            sdList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());
            //sdList.Add("FamilyMember", txtFamilyMember.Text.Trim());
            //sdList.Add("JobExperience", txtJobExperience.Text.Trim());
            //if (stateName == "离职")
            //{
            //    if (RegexRule.IsDate(outTime))
            //    {
            //        sdList.Add("OutTime", outTime);
            //    }
            //}
            sdList.Add("HealthTime", txtHealthTime.Text.Trim());
            sdList.Add("SecurityAmount", (txtSecurityAmount.Text.Trim() == "") ? "0" : txtSecurityAmount.Text.Trim());
            sdList.Add("ManageAmount", (txtManageAmount.Text.Trim() == "") ? "0" : txtManageAmount.Text.Trim());
       

            sdList.Add("SpecialLinkMan", txtSpecialLinkMan.Text.Trim());
            sdList.Add("SpecialLinkWay", txtSpecialLinkWay.Text.Trim());
            sdList.Add("SpecialRelation", txtSpecialRelation.Text.Trim());

            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);

            if (salesmanBiz.Insert("Admin_Salesman", sdList))
               
                JSUtility.AlertAndRedirect("增加信息成功", "SalesmanAdd.aspx");

            else
                JSUtility.Alert(this, "增加信息失败，请检查工号是否已经被使用");
               
 
        }
      
        
        #region 绑定数据
        //根据ID绑定
        private void BindDataByID(string saleManID)
        {
            DataTable dt = salesmanBiz.GetOneInfo(saleManID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                defDicBiz.InitDropDownList(ddlEmpSex, dt.Rows[0]["EmpSex"].ToString());
                txtRegistry.Text = dt.Rows[0]["Registry"].ToString();
                txtNationality.Text = dt.Rows[0]["Nationality"].ToString();
                txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();
                txtAddr.Text = dt.Rows[0]["Addr"].ToString();

                txtHomeTel.Text = dt.Rows[0]["HomeTel"].ToString();
                txtMobile.Text = dt.Rows[0]["Mobile"].ToString();
                txtEducationSchool.Text = dt.Rows[0]["EducationSchool"].ToString();
                defDicBiz.InitDropDownList("Admin_Emp_Education", ddlEducationID, dt.Rows[0]["EducationHigh"].ToString().Trim());
                defDicBiz.InitDropDownList("Admin_Salesman_Type", ddlTypeName, dt.Rows[0]["TypeName"].ToString());

                defDicBiz.InitDropDownList(ddlStateName, dt.Rows[0]["StateName"].ToString());
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtBrand.Text = dt.Rows[0]["DutyName"].ToString();

                txtSpecialLinkMan.Text = dt.Rows[0]["SpecialLinkMan"].ToString();
                txtSpecialLinkWay.Text = dt.Rows[0]["SpecialLinkWay"].ToString();
                txtSpecialRelation.Text = dt.Rows[0]["SpecialRelation"].ToString();

                string joinTime = dt.Rows[0]["JoinTime"].ToString();
                if (Utils.RegexRule.IsDate(joinTime))
                    txtJoinTime.Text = Convert.ToDateTime(joinTime).ToString("yyyy-MM-dd");
               
   
                txtFromCompanyName.Text = dt.Rows[0]["FromCompanyName"].ToString();

                string healthTime = dt.Rows[0]["HealthTime"].ToString();
                if (Utils.RegexRule.IsDate(healthTime))
                    txtHealthTime.Text = Convert.ToDateTime(healthTime).ToString("yyyy-MM-dd");

                txtSecurityAmount.Text = dt.Rows[0]["SecurityAmount"].ToString();
                txtManageAmount.Text = dt.Rows[0]["ManageAmount"].ToString();

                string outTime = dt.Rows[0]["OutTime"].ToString();
                if (Utils.RegexRule.IsDate(outTime))
                    txtOutTime.Text = Convert.ToDateTime(outTime).ToString("yyyy-MM-dd");

                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
            }

        }
        //根据工号绑定数据
        private void BindData(string empCode)
        {
          DataTable dt=  salesmanBiz.GetOneInfo(empCode,"");
          if (dt.Rows.Count > 0)
          {
              if (txtEmpCode.Text=="")  txtEmpCode.Text = empCode;
              txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
              defDicBiz.InitDropDownList(ddlEmpSex,dt.Rows[0]["EmpSex"].ToString());
              txtRegistry.Text = dt.Rows[0]["Registry"].ToString();
              txtNationality.Text = dt.Rows[0]["Nationality"].ToString();
              txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();
              txtAddr.Text = dt.Rows[0]["Addr"].ToString();

              txtHomeTel.Text = dt.Rows[0]["HomeTel"].ToString();
              txtMobile.Text = dt.Rows[0]["Mobile"].ToString();
              txtEducationSchool.Text = dt.Rows[0]["EducationSchool"].ToString();
              defDicBiz.InitDropDownList("Admin_Emp_Education", ddlEducationID, dt.Rows[0]["EducationHigh"].ToString());
            

              defDicBiz.InitDropDownList("Admin_Salesman_Type", ddlTypeName, dt.Rows[0]["TypeName"].ToString());
              defDicBiz.InitDropDownList(ddlEmpSex,dt.Rows[0]["StateName"].ToString());
              txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
              txtBrand.Text = dt.Rows[0]["DutyName"].ToString();

              txtSpecialLinkMan.Text = dt.Rows[0]["SpecialLinkMan"].ToString();
              txtSpecialLinkWay.Text = dt.Rows[0]["SpecialLinkWay"].ToString();
              txtSpecialRelation.Text = dt.Rows[0]["SpecialRelation"].ToString();
             
              string joinTime = dt.Rows[0]["JoinTime"].ToString();
              if (Utils.RegexRule.IsDate(joinTime))
                  txtJoinTime.Text = Convert.ToDateTime(joinTime).ToString("yyyy-MM-dd");
             

              txtFromCompanyName.Text = dt.Rows[0]["FromCompanyName"].ToString();

              string healthTime = dt.Rows[0]["HealthTime"].ToString();
              if (Utils.RegexRule.IsDate(healthTime))
                  txtHealthTime.Text = Convert.ToDateTime(healthTime).ToString("yyyy-MM-dd");   
             
              txtSecurityAmount.Text = dt.Rows[0]["SecurityAmount"].ToString();
              txtManageAmount.Text = dt.Rows[0]["ManageAmount"].ToString();

              string outTime = dt.Rows[0]["OutTime"].ToString();
              if (Utils.RegexRule.IsDate(outTime))
                  txtOutTime.Text = Convert.ToDateTime(outTime).ToString("yyyy-MM-dd");   

              txtMemo.Text=dt.Rows[0]["Memo"].ToString();
          }
          
        }
        #endregion


      

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string typeName = ddlTypeName.SelectedItem.Text.Trim();
            if (typeName == "请选择")
            {
                JSUtility.Alert(this, "请选择工作类别！");
                return;
            }
            string educationHigh = ddlEducationID.SelectedItem.Text.Trim();
            if (educationHigh == "请选择")
            {
                JSUtility.Alert(this, "请选择最高学历！");
                return;
            }
            string stateName = ddlStateName.SelectedItem.Text.Trim();
            string outTime = txtOutTime.Text.Trim();
            if (stateName == "离职")
            {
                    JSUtility.Alert(this, "离职状态数据，不能更新！");
                    return;              
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("SaleManID", saleManID);
            sdList.Add("EmpCode", txtEmpCode.Text.Trim());
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("EmpSex", ddlEmpSex.SelectedItem.Text.Trim());
            sdList.Add("Registry", txtRegistry.Text.Trim());
            sdList.Add("Nationality", txtNationality.Text.Trim());
            sdList.Add("ZipCode", txtZipCode.Text.Trim());
            sdList.Add("Addr", txtAddr.Text.Trim());
            sdList.Add("HomeTel", txtHomeTel.Text.Trim());
            sdList.Add("Mobile", txtMobile.Text.Trim());
            sdList.Add("EducationSchool", txtEducationSchool.Text.Trim());
            sdList.Add("EducationHigh", educationHigh);
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtBrand.Text.Trim());
            sdList.Add("TypeName", typeName);
            sdList.Add("StateName", ddlStateName.SelectedItem.Text.Trim());  

            //if (stateName == "离职")
            //{
            //    if (RegexRule.IsDate(outTime))
            //    {
            //        sdList.Add("OutTime", outTime);
            //    }
            //}
            sdList.Add("JoinTime", txtJoinTime.Text.Trim());
            sdList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());
          
            sdList.Add("HealthTime", txtHealthTime.Text.Trim());
            sdList.Add("SecurityAmount", (txtSecurityAmount.Text.Trim() == "") ? "0" : txtSecurityAmount.Text.Trim());
            sdList.Add("ManageAmount", (txtManageAmount.Text.Trim() == "") ? "0" : txtManageAmount.Text.Trim());
       
            sdList.Add("SpecialLinkMan", txtSpecialLinkMan.Text.Trim());
            sdList.Add("SpecialLinkWay", txtSpecialLinkWay.Text.Trim());
            sdList.Add("SpecialRelation", txtSpecialRelation.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
             string info = "";
             if (salesmanBiz.Update("Admin_Salesman", sdList))           
                info = "更新信息成功";
            else
                info = "更新信息失败";
            JSUtility.Alert(this, info);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            
             if (!string.IsNullOrWhiteSpace(saleManID))
            {
                if (salesmanBiz.DeleteOneInfo(saleManID))
                JSUtility.Alert(this,"删除成功");
            else
                JSUtility.Alert(this,"删除失败");
            }
            else
                JSUtility.Alert(this,"无此工号");
        }



        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {

            string empCode2 = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode2))
            {
                BindData(empCode2);
            }

        }

       
    }
}